import Base from './BaseElement';
import Paint from '../Paint';
import CanvasUtils from '@/utils/CanvasUtils';
import { RoundRectI, SpotI } from '@/utils/CanvasUtils/CanvasType';

export default class RoundRectElement extends Base {

	public roundRect: RoundRectI;

	private save: RoundRectI | null = null;

	public constructor(paint: Paint, roundRect: RoundRectI) {
		super(paint)
		this.roundRect = roundRect;
	}

	public Draw(canvas: CanvasUtils): void {
		const { Rect: { Spot: { X, Y }, Width, Height }, Radius, BackgroundColor, BorderColor, LineWidth } = this.roundRect;
		const save = this.save = { Rect: { Spot: { X, Y }, Width, Height }, Radius, BackgroundColor, BorderColor, LineWidth };
		canvas.Save();
		if (BackgroundColor) {
			canvas.FillStyle = BackgroundColor;
		}
		if (BorderColor) {
			if (LineWidth) {
				canvas.LineWidth = LineWidth;
			}
			canvas.StrokeStyle = BorderColor;
		}
		canvas.RoundRect(save);
		canvas.Restore();
	}

	public OnRange({ X: x, Y: y }: SpotI): boolean {
		if (!this.save) {
			return false;
		}
		const { Spot: { X, Y }, Width, Height } = this.save.Rect;
		return x >= X && x <= X + Width && y >= Y && y <= Y + Height;
	}
}
